home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 001a / rzsz9107.zip / SZ.C < prev    next >
C/C++ Source or Header  |  1991-07-02  |  38KB  |  1,770 lines

  1. #define VERSION "3.14 07-02-91"
  2. #define PUBDIR "/usr/spool/uucppublic"
  3.  
  4. /*% cc -compat -M2 -Ox -K -i -DTXBSIZE=16384  -DNFGVMIN -DREADCHECK sz.c -lx -o sz; size sz
  5.  
  6. <-xtx-*> cc -Osal -DTXBSIZE=32768  -DSV sz.c -lx -o $B/sz; size $B/sz
  7.  
  8.  ****************************************************************************
  9.  *
  10.  * sz.c By Chuck Forsberg,  Omen Technology INC
  11.  *
  12.  ****************************************************************************
  13.  *
  14.  * Typical Unix/Xenix/Clone compiles:
  15.  *
  16.  *    cc -O sz.c -o sz        USG (SYS III/V) Unix
  17.  *    cc -O -DSV sz.c -o sz        Sys V Release 2 with non-blocking input
  18.  *                    Define to allow reverse channel checking
  19.  *    cc -O -DV7  sz.c -o sz        Unix Version 7, 2.8 - 4.3 BSD
  20.  *
  21.  *    cc -O -K -i -DNFGVMIN -DREADCHECK sz.c -lx -o sz    Classic Xenix
  22.  *
  23.  *    ln sz sb            **** All versions ****
  24.  *    ln sz sx            **** All versions ****
  25.  *
  26.  ****************************************************************************
  27.  ****************************************************************************
  28.  *
  29.  *
  30.  * A program for Unix to send files and commands to computers running
  31.  *  Professional-YAM, PowerCom, YAM, IMP, or programs supporting Y/XMODEM.
  32.  *
  33.  *  Sz uses buffered I/O to greatly reduce CPU time compared to UMODEM.
  34.  *
  35.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  36.  *
  37.  * 
  38.  *    This version implements numerous enhancements including ZMODEM
  39.  *    Run Length Encoding and variable length headers.  These
  40.  *    features were not funded by the original Telenet development
  41.  *    contract.
  42.  * 
  43.  * This software may be freely used for non commercial and
  44.  * educational (didactic only) purposes.  This software may also
  45.  * be freely used to support file transfer operations to or from
  46.  * licensed Omen Technology products.  Any programs which use
  47.  * part or all of this software must be provided in source form
  48.  * with this notice intact except by written permission from Omen
  49.  * Technology Incorporated.
  50.  * 
  51.  * Use of this software for commercial or administrative purposes
  52.  * except when exclusively limited to interfacing Omen Technology
  53.  * products requires a per port license payment of $20.00 US per
  54.  * port (less in quantity).  Use of this code by inclusion,
  55.  * decompilation, reverse engineering or any other means
  56.  * constitutes agreement to these conditions and acceptance of
  57.  * liability to license the materials and payment of reasonable
  58.  * legal costs necessary to enforce this license agreement.
  59.  *
  60.  *
  61.  *        Omen Technology Inc        FAX: 503-621-3745
  62.  *        Post Office Box 4681
  63.  *        Portland OR 97208
  64.  *
  65.  *    This code is made available in the hope it will be useful,
  66.  *    BUT WITHOUT ANY WARRANTY OF ANY KIND OR LIABILITY FOR ANY
  67.  *    DAMAGES OF ANY KIND.
  68.  */
  69.  
  70.  
  71. char *substr(), *getenv();
  72.  
  73. #define LOGFILE "/tmp/szlog"
  74. #include <stdio.h>
  75. #include <signal.h>
  76. #include <setjmp.h>
  77. #include <ctype.h>
  78. #include <errno.h>
  79. extern int errno;
  80. #define STATIC
  81.  
  82. #define sendline(c) putchar(c & 0377)
  83. #define xsendline(c) putchar(c)
  84.  
  85. #define PATHLEN 256
  86. #define OK 0
  87. #define FALSE 0
  88. #ifdef TRUE
  89. #undef TRUE
  90. #endif
  91. #define TRUE 1
  92. #define ERROR (-1)
  93. /* Ward Christensen / CP/M parameters - Don't change these! */
  94. #define ENQ 005
  95. #define CAN ('X'&037)
  96. #define XOFF ('s'&037)
  97. #define XON ('q'&037)
  98. #define SOH 1
  99. #define STX 2
  100. #define EOT 4
  101. #define ACK 6
  102. #define NAK 025
  103. #define CPMEOF 032
  104. #define WANTCRC 0103    /* send C not NAK to get crc not checksum */
  105. #define WANTG 0107    /* Send G not NAK to get nonstop batch xmsn */
  106. #define TIMEOUT (-2)
  107. #define RCDO (-3)
  108. #define GCOUNT (-4)
  109. #define RETRYMAX 10
  110.  
  111.  
  112. #define HOWMANY 2
  113. STATIC int Zmodem=0;        /* ZMODEM protocol requested by receiver */
  114. unsigned Baudrate=4800;        /* Default, set by first mode() call */
  115. STATIC unsigned Effbaud = 4800;
  116. STATIC unsigned Txwindow;    /* Control the size of the transmitted window */
  117. STATIC unsigned Txwspac;    /* Spacing between zcrcq requests */
  118. STATIC unsigned Txwcnt;    /* Counter used to space ack requests */
  119. STATIC long Lrxpos;        /* Receiver's last reported offset */
  120. STATIC int errors;
  121. char endmsg[80] = {0};    /* Possible message to display on exit */
  122.  
  123. #include "rbsb.c"    /* most of the system dependent stuff here */
  124.  
  125. #include "crctab.c"
  126.  
  127. STATIC int Filesleft;
  128. STATIC long Totalleft;
  129.  
  130. /*
  131.  * Attention string to be executed by receiver to interrupt streaming data
  132.  *  when an error is detected.  A pause (0336) may be needed before the
  133.  *  ^C (03) or after it.
  134.  */
  135. #ifdef READCHECK
  136. STATIC char Myattn[] = { 0 };
  137. #else
  138. #ifdef USG
  139. STATIC char Myattn[] = { 03, 0336, 0 };
  140. #endif
  141. #endif
  142.  
  143. FILE *in;
  144.  
  145. #ifdef BADSEEK
  146. STATIC int Canseek = 0;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  147. #ifndef TXBSIZE
  148. #define TXBSIZE 16384        /* Must be power of two, < MAXINT */
  149. #endif
  150. #else
  151. STATIC int Canseek = 1;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  152. #endif
  153.  
  154. #ifdef TXBSIZE
  155. #define TXBMASK (TXBSIZE-1)
  156. STATIC char Txb[TXBSIZE];        /* Circular buffer for file reads */
  157. STATIC char *txbuf = Txb;        /* Pointer to current file segment */
  158. #else
  159. STATIC char txbuf[1024];
  160. #endif
  161. STATIC long vpos = 0;            /* Number of bytes read from file */
  162.  
  163. STATIC char Lastrx;
  164. STATIC char Crcflg;
  165. STATIC int Verbose=0;
  166. STATIC int Modem2=0;        /* XMODEM Protocol - don't send pathnames */
  167. STATIC int Restricted=0;    /* restricted; no /.. or ../ in filenames */
  168. STATIC int Quiet=0;        /* overrides logic that would otherwise set verbose */
  169. STATIC int Ascii=0;        /* Add CR's for brain damaged programs */
  170. STATIC int Fullname=0;        /* transmit full pathname */
  171. STATIC int Unlinkafter=0;    /* Unlink file after it is sent */
  172. STATIC int Dottoslash=0;    /* Change foo.bar.baz to foo/bar/baz */
  173. STATIC int firstsec;
  174. STATIC int errcnt=0;        /* number of files unreadable */
  175. STATIC int blklen=128;        /* length of transmitted records */
  176. STATIC int Optiong;        /* Let it rip no wait for sector ACK's */
  177. STATIC int Eofseen;        /* EOF seen on input set by zfilbuf */
  178. STATIC int BEofseen;        /* EOF seen on input set by fooseek */
  179. STATIC int Totsecs;        /* total number of sectors this file */
  180. STATIC int Filcnt=0;        /* count of number of files opened */
  181. STATIC int Lfseen=0;
  182. STATIC unsigned Rxbuflen = 16384;    /* Receiver's max buffer length */
  183. STATIC int Tframlen = 0;    /* Override for tx frame length */
  184. STATIC int blkopt=0;        /* Override value for zmodem blklen */
  185. STATIC int Rxflags = 0;
  186. STATIC long bytcnt;
  187. STATIC int Wantfcs32 = TRUE;    /* want to send 32 bit FCS */
  188. STATIC char Lzconv;    /* Local ZMODEM file conversion request */
  189. STATIC char Lzmanag;    /* Local ZMODEM file management request */
  190. STATIC int Lskipnocor;
  191. STATIC char Lztrans;
  192. STATIC int Command;        /* Send a command, then exit. */
  193. STATIC char *Cmdstr;        /* Pointer to the command string */
  194. STATIC int Cmdtries = 11;
  195. STATIC int Cmdack1;        /* Rx ACKs command, then do it */
  196. STATIC int Exitcode;
  197. STATIC int Test;        /* 1= Force receiver to send Attn, etc with qbf. */
  198.             /* 2= Character transparency test */
  199. STATIC char *qbf=
  200.  "The quick brown fox jumped over the lazy dog's back 1234567890\r\n";
  201. STATIC long Lastsync;        /* Last offset to which we got a ZRPOS */
  202. STATIC int Beenhereb4;        /* How many times we've been ZRPOS'd same place */
  203.  
  204. STATIC jmp_buf tohere;        /* For the interrupt on RX timeout */
  205. STATIC jmp_buf intrjmp;    /* For the interrupt on RX CAN */
  206.  
  207. #ifdef XARGSFILE
  208. char *
  209. mystrsave(s)
  210. char *s;
  211. {
  212.     register char *p;
  213.     char *malloc();
  214.  
  215.     if (p = malloc(strlen(s)+1) ) {
  216.         strcpy(p, s); return p;
  217.     }
  218.     fprintf(stderr, "No memory for mystrsave!\n");
  219.     exit(1);
  220. }
  221.  
  222. /* Remove (presumably) terminating CR and/or LF from string */
  223. uncrlf(s)
  224. register char *s;
  225. {
  226.     for ( ; *s; ++s)
  227.         switch (*s) {
  228.         case '\r':
  229.         case '\n':
  230.             *s = 0;  return;
  231.         }
  232. }
  233. #endif
  234.  
  235.  
  236. /* called by signal interrupt or terminate to clean things up */
  237. bibi(n)
  238. {
  239.     canit(); fflush(stdout); mode(0);
  240.     fprintf(stderr, "sz: caught signal %d; exiting\n", n);
  241.     if (n == SIGQUIT)
  242.         abort();
  243.     if (n == 99)
  244.         fprintf(stderr, "mode(2) in rbsb.c not implemented!!\n");
  245.     cucheck();
  246.     exit(128+n);
  247. }
  248. /* Called when ZMODEM gets an interrupt (^X) */
  249. onintr()
  250. {
  251.     signal(SIGINT, SIG_IGN);
  252.     longjmp(intrjmp, -1);
  253. }
  254.  
  255. STATIC int Zctlesc;    /* Encode control characters */
  256. STATIC int Nozmodem = 0;    /* If invoked as "sb" */
  257. STATIC char *Progname = "sz";
  258. STATIC int Zrwindow = 1400;    /* RX window size (controls garbage count) */
  259. #include "zm.c"
  260.  
  261. #include "zmr.c"
  262.  
  263. #ifdef XARGSFILE
  264. #define XARGSMAX 256
  265. char *xargv[XARGSMAX+1];
  266. #endif
  267.  
  268. main(argc, argv)
  269. char *argv[];
  270. {
  271.     register char *cp;
  272.     register npats;
  273.     int dm;
  274.     char **patts;
  275.     static char xXbuf[BUFSIZ];
  276.  
  277.     if ((cp = getenv("ZNULLS")) && *cp)
  278.         Znulls = atoi(cp);
  279.     if ((cp=getenv("SHELL")) && (substr(cp, "rsh") || substr(cp, "rksh")))
  280.         Restricted=TRUE;
  281.     from_cu();
  282.     chkinvok(argv[0]);
  283.  
  284.     Rxtimeout = 600;
  285.     npats=0;
  286.     if (argc<2)
  287.         usage();
  288.     setbuf(stdout, xXbuf);        
  289.     while (--argc) {
  290.         cp = *++argv;
  291.         if (*cp++ == '-' && *cp) {
  292.             while ( *cp) {
  293.                 switch(*cp++) {
  294.                 case '\\':
  295.                      *cp = toupper(*cp);  continue;
  296.                 case '+':
  297.                     Lzmanag = ZMAPND; break;
  298. #ifdef CSTOPB
  299.                 case '2':
  300.                     Twostop = TRUE; break;
  301. #endif
  302.                 case 'a':
  303.                     Lzconv = ZCNL;
  304.                     Ascii = TRUE; break;
  305.                 case 'b':
  306.                     Lzconv = ZCBIN; break;
  307.                 case 'C':
  308.                     if (--argc < 1) {
  309.                         usage();
  310.                     }
  311.                     Cmdtries = atoi(*++argv);
  312.                     break;
  313.                 case 'i':
  314.                     Cmdack1 = ZCACK1;
  315.                     /* **** FALL THROUGH TO **** */
  316.                 case 'c':
  317.                     if (--argc != 1) {
  318.                         usage();
  319.                     }
  320.                     Command = TRUE;
  321.                     Cmdstr = *++argv;
  322.                     break;
  323.                 case 'd':
  324.                     ++Dottoslash;
  325.                     /* **** FALL THROUGH TO **** */
  326.                 case 'f':
  327.                     Fullname=TRUE; break;
  328.                 case 'e':
  329.                     Zctlesc = 1; break;
  330.                 case 'k':
  331.                     blklen=1024; break;
  332.                 case 'L':
  333.                     if (--argc < 1) {
  334.                         usage();
  335.                     }
  336.                     blkopt = atoi(*++argv);
  337.                     if (blkopt<24 || blkopt>1024)
  338.                         usage();
  339.                     break;
  340.                 case 'l':
  341.                     if (--argc < 1) {
  342.                         usage();
  343.                     }
  344.                     Tframlen = atoi(*++argv);
  345.                     if (Tframlen<32 || Tframlen>1024)
  346.                         usage();
  347.                     break;
  348.                 case 'N':
  349.                     Lzmanag = ZMNEWL;  break;
  350.                 case 'n':
  351.                     Lzmanag = ZMNEW;  break;
  352.                 case 'o':
  353.                     Wantfcs32 = FALSE; break;
  354.                 case 'p':
  355.                     Lzmanag = ZMPROT;  break;
  356.                 case 'r':
  357.                     if (Lzconv == ZCRESUM)
  358.                         Lzmanag = (Lzmanag & ZMMASK) | ZMCRC;
  359.                     Lzconv = ZCRESUM; break;
  360.                 case 'q':
  361.                     Quiet=TRUE; Verbose=0; break;
  362.                 case 't':
  363.                     if (--argc < 1) {
  364.                         usage();
  365.                     }
  366.                     Rxtimeout = atoi(*++argv);
  367.                     if (Rxtimeout<10 || Rxtimeout>1000)
  368.                         usage();
  369.                     break;
  370.                 case 'T':
  371.                     if (++Test > 1) {
  372.                         chartest(1); chartest(2);
  373.                         mode(0);  exit(0);
  374.                     }
  375.                     break;
  376.                 case 'u':
  377.                     ++Unlinkafter; break;
  378.                 case 'v':
  379.                     ++Verbose; break;
  380.                 case 'w':
  381.                     if (--argc < 1) {
  382.                         usage();
  383.                     }
  384.                     Txwindow = atoi(*++argv);
  385.                     if (Txwindow < 256)
  386.                         Txwindow = 256;
  387.                     Txwindow = (Txwindow/64) * 64;
  388.                     Txwspac = Txwindow/4;
  389.                     if (blkopt > Txwspac
  390.                      || (!blkopt && Txwspac < 1024))
  391.                         blkopt = Txwspac;
  392.                     break;
  393.                 case 'X':
  394.                     ++Modem2; break;
  395.                 case 'Y':
  396.                     Lskipnocor = TRUE;
  397.                     /* **** FALLL THROUGH TO **** */
  398.                 case 'y':
  399.                     Lzmanag = ZMCLOB; break;
  400.                 case 'Z':
  401.                 case 'z':
  402.                     Lztrans = ZTRLE;  break;
  403.                 default:
  404.                     usage();
  405.                 }
  406.             }
  407.         }
  408.         else if ( !npats && argc>0) {
  409.             if (argv[0][0]) {
  410.                 npats=argc;
  411.                 patts=argv;
  412.             }
  413.         }
  414.     }
  415.     if (npats < 1 && !Command && !Test) 
  416.         usage();
  417.     if (Verbose) {
  418.         if (freopen(LOGFILE, "a", stderr)==NULL) {
  419.             printf("Can't open log file %s\n",LOGFILE);
  420.             exit(0200);
  421.         }
  422.         setbuf(stderr, NULL);
  423.     }
  424.     if (Fromcu && !Quiet) {
  425.         if (Verbose == 0)
  426.             Verbose = 2;
  427.     }
  428.     vfile("%s %s for %s\n", Progname, VERSION, OS);
  429.  
  430. #ifdef XARGSFILE
  431.     vfile("npats=%d *patts=%s", npats, *patts);
  432.     if (npats == 1 && !strcmp(XARGSFILE, *patts)) {
  433.         in = fopen(XARGSFILE, "r");
  434.         if (!in) {
  435.             printf(stderr, "Can't open / control file!\n");
  436.             exit(2);
  437.         }
  438.         for (npats=0,argv=patts=xargv; npats<XARGSMAX; ++npats,++argv) {
  439.             if (fgets(txbuf, 1024, in) <= 0)
  440.                 break;
  441.             uncrlf(txbuf);
  442.             *argv = mystrsave(txbuf);
  443.         }
  444.         fclose(in);
  445.     }
  446. #endif
  447.  
  448.     mode(1);
  449.  
  450.     if (signal(SIGINT, bibi) == SIG_IGN) {
  451.         signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
  452.     } else {
  453.         signal(SIGINT, bibi); signal(SIGKILL, bibi);
  454.     }
  455. #ifdef SIGQUIT
  456.     if ( !Fromcu)
  457.         signal(SIGQUIT, SIG_IGN);
  458. #endif
  459. #ifdef SIGTERM
  460.     signal(SIGTERM, bibi);
  461. #endif
  462.  
  463.     if ( !Modem2) {
  464.         if (!Nozmodem) {
  465.             printf("rz\r");  fflush(stdout);
  466.         }
  467.         countem(npats, patts);
  468.         if (!Nozmodem) {
  469.             stohdr(0L);
  470.             if (Command)
  471.                 Txhdr[ZF0] = ZCOMMAND;
  472.             zshhdr(4, ZRQINIT, Txhdr);
  473.         }
  474.     }
  475.     fflush(stdout);
  476.  
  477.     if (Command) {
  478.         if (getzrxinit()) {
  479.             Exitcode=0200; canit();
  480.         }
  481.         else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  482.             Exitcode=0200; canit();
  483.         }
  484.     } else if (wcsend(npats, patts)==ERROR) {
  485.         Exitcode=0200;
  486.         canit();
  487.     }
  488.     if (endmsg[0])
  489.         printf("  %s: %s\r\n", Progname, endmsg);
  490.     printf("%s %s finished.\r\n", Progname, VERSION);
  491.     fflush(stdout);
  492.     mode(0);
  493.     dm = ((errcnt != 0) | Exitcode);
  494.     if (dm)
  495.         cucheck();
  496.     exit(dm);
  497.     /*NOTREACHED*/
  498. }
  499.  
  500. wcsend(argc, argp)
  501. char *argp[];
  502. {
  503.     register n;
  504.  
  505.     Crcflg=FALSE;
  506.     firstsec=TRUE;
  507.     bytcnt = -1;
  508.     if (Nozmodem) {
  509.         printf("Start your YMODEM receive. ");  fflush(stdout);
  510.     }
  511.     for (n=0; n<argc; ++n) {
  512.         Totsecs = 0;
  513.         if (wcs(argp[n])==ERROR)
  514.             return ERROR;
  515.     }
  516.     Totsecs = 0;
  517.     if (Filcnt==0) {    /* bitch if we couldn't open ANY files */
  518.         if (!Nozmodem && !Modem2) {
  519.             Command = TRUE;
  520.             Cmdstr = "echo \"sz: Can't open any requested files\"";
  521.             if (getnak()) {
  522.                 Exitcode=0200; canit();
  523.             }
  524.             if (!Zmodem)
  525.                 canit();
  526.             else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  527.                 Exitcode=0200; canit();
  528.             }
  529.             Exitcode = 1; return OK;
  530.         }
  531.         canit();
  532.         sprintf(endmsg, "Can't open any requested files");
  533.         return ERROR;
  534.     }
  535.     if (Zmodem)
  536.         saybibi();
  537.     else if ( !Modem2)
  538.         wctxpn("");
  539.     return OK;
  540. }
  541.  
  542. wcs(oname)
  543. char *oname;
  544. {
  545.     register c;
  546.     register char *p, *q;
  547.     struct stat f;
  548.     char name[PATHLEN];
  549.  
  550.     strcpy(name, oname);
  551.  
  552.     if (Restricted) {
  553.         /* restrict pathnames to current tree or uucppublic */
  554.         if ( substr(name, "../")
  555.          || (name[0]== '/' && strncmp(name, PUBDIR, strlen(PUBDIR))) ) {
  556.             canit();  sprintf(endmsg,"Security Violation");
  557.             return ERROR;
  558.         }
  559.     }
  560.  
  561.     in=fopen(oname, ROPMODE);
  562.  
  563.     if (in==NULL) {
  564.         ++errcnt;
  565.         return OK;    /* pass over it, there may be others */
  566.     }
  567.     BEofseen = Eofseen = 0;  vpos = 0;
  568.  
  569.     /* Check for directory or block special files */
  570.     fstat(fileno(in), &f);
  571.     c = f.st_mode & S_IFMT;
  572.     if (c == S_IFDIR || c == S_IFBLK) {
  573.         fclose(in);
  574.         return OK;
  575.     }
  576.  
  577.     ++Filcnt;
  578.     switch (wctxpn(name)) {
  579.     case ERROR:
  580.         return ERROR;
  581.     case ZSKIP:
  582.         return OK;
  583.     }
  584.     if (!Zmodem && wctx(f.st_size)==ERROR)
  585.         return ERROR;
  586.  
  587.     if (Unlinkafter)
  588.         unlink(oname);
  589.  
  590.     return 0;
  591. }
  592.  
  593. /*
  594.  * generate and transmit pathname block consisting of
  595.  *  pathname (null terminated),
  596.  *  file length, mode time and file mode in octal
  597.  *  as provided by the Unix fstat call.
  598.  *  N.B.: modifies the passed name, may extend it!
  599.  */
  600. wctxpn(name)
  601. char *name;
  602. {
  603.     register char *p, *q;
  604.     char name2[PATHLEN];
  605.     struct stat f;
  606.  
  607.     if (Modem2) {
  608.         if (*name && fstat(fileno(in), &f)!= -1) {
  609.             fprintf(stderr, "Sending %s, %ld XMODEM blocks. ",
  610.               name, (127+f.st_size)>>7);
  611.         }
  612.         fprintf(stderr, "Give your local XMODEM receive command now.\r\n");
  613.         fflush(stderr);
  614.         return OK;
  615.     }
  616.     zperr("Awaiting pathname nak for %s", *name?name:"<END>");
  617.     if ( !Zmodem)
  618.         if (getnak())
  619.             return ERROR;
  620.  
  621.     q = (char *) 0;
  622.     if (Dottoslash) {        /* change . to . */
  623.         for (p=name; *p; ++p) {
  624.             if (*p == '/')
  625.                 q = p;
  626.             else if (*p == '.')
  627.                 *(q=p) = '/';
  628.         }
  629.         if (q && strlen(++q) > 8) {    /* If name>8 chars */
  630.             q += 8;            /*   make it .ext */
  631.             strcpy(name2, q);    /* save excess of name */
  632.             *q = '.';
  633.             strcpy(++q, name2);    /* add it back */
  634.         }
  635.     }
  636.  
  637.     for (p=name, q=txbuf ; *p; )
  638.         if ((*q++ = *p++) == '/' && !Fullname)
  639.             q = txbuf;
  640.     *q++ = 0;
  641.     p=q;
  642.     while (q < (txbuf + 1024))
  643.         *q++ = 0;
  644.     if (*name) {
  645.         if (fstat(fileno(in), &f)!= -1)
  646.             sprintf(p, "%lu %lo %o 0 %d %ld", f.st_size, f.st_mtime,
  647.               f.st_mode, Filesleft, Totalleft);
  648.         Totalleft -= f.st_size;
  649.     }
  650.     if (--Filesleft <= 0)
  651.         Filesleft = Totalleft = 0;
  652.     if (Totalleft < 0)
  653.         Totalleft = 0;
  654.  
  655.     /* force 1k blocks if name won't fit in 128 byte block */
  656.     if (txbuf[125])
  657.         blklen=1024;
  658.     else {        /* A little goodie for IMP/KMD */
  659.         txbuf[127] = (f.st_size + 127) >>7;
  660.         txbuf[126] = (f.st_size + 127) >>15;
  661.     }
  662.     if (Zmodem)
  663.         return zsendfile(txbuf, 1+strlen(p)+(p-txbuf));
  664.     if (wcputsec(txbuf, 0, 128)==ERROR)
  665.         return ERROR;
  666.     return OK;
  667. }
  668.  
  669. getnak()
  670. {
  671.     register firstch;
  672.  
  673.     Lastrx = 0;
  674.     for (;;) {
  675.         switch (firstch = readline(800)) {
  676.         case ZPAD:
  677.             if (getzrxinit())
  678.                 return ERROR;
  679.             Ascii = 0;    /* Receiver does the conversion */
  680.             return FALSE;
  681.         case TIMEOUT:
  682.             sprintf(endmsg, "Timeout waiting for ZRINIT");
  683.             return TRUE;
  684.         case WANTG:
  685. #ifdef MODE2OK
  686.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  687. #endif
  688.             Optiong = TRUE;
  689.             blklen=1024;
  690.         case WANTCRC:
  691.             Crcflg = TRUE;
  692.         case NAK:
  693.             return FALSE;
  694.         case CAN:
  695.             if ((firstch = readline(20)) == CAN && Lastrx == CAN) {
  696.                 sprintf(endmsg, "Got CAN waiting to send file");
  697.                 return TRUE;
  698.             }
  699.         default:
  700.             break;
  701.         }
  702.         Lastrx = firstch;
  703.     }
  704. }
  705.  
  706.  
  707. wctx(flen)
  708. long flen;
  709. {
  710.     register int thisblklen;
  711.     register int sectnum, attempts, firstch;
  712.     long charssent;
  713.  
  714.     charssent = 0;  firstsec=TRUE;  thisblklen = blklen;
  715.     vfile("wctx:file length=%ld", flen);
  716.  
  717.     while ((firstch=readline(Rxtimeout))!=NAK && firstch != WANTCRC
  718.       && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
  719.         ;
  720.     if (firstch==CAN) {
  721.         zperr("Receiver CANcelled");
  722.         return ERROR;
  723.     }
  724.     if (firstch==WANTCRC)
  725.         Crcflg=TRUE;
  726.     if (firstch==WANTG)
  727.         Crcflg=TRUE;
  728.     sectnum=0;
  729.     for (;;) {
  730.         if (flen <= (charssent + 896L))
  731.             thisblklen = 128;
  732.         if ( !filbuf(txbuf, thisblklen))
  733.             break;
  734.         if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)
  735.             return ERROR;
  736.         charssent += thisblklen;
  737.     }
  738.     fclose(in);
  739.     attempts=0;
  740.     do {
  741.         purgeline();
  742.         sendline(EOT);
  743.         flushmo();
  744.         ++attempts;
  745.     }
  746.         while ((firstch=(readline(Rxtimeout)) != ACK) && attempts < RETRYMAX);
  747.     if (attempts == RETRYMAX) {
  748.         zperr("No ACK on EOT");
  749.         return ERROR;
  750.     }
  751.     else
  752.         return OK;
  753. }
  754.  
  755. wcputsec(buf, sectnum, cseclen)
  756. char *buf;
  757. int sectnum;
  758. int cseclen;    /* data length of this sector to send */
  759. {
  760.     register checksum, wcj;
  761.     register char *cp;
  762.     unsigned oldcrc;
  763.     int firstch;
  764.     int attempts;
  765.  
  766.     firstch=0;    /* part of logic to detect CAN CAN */
  767.  
  768.     if (Verbose>2)
  769.         fprintf(stderr, "Sector %3d %2dk\n", Totsecs, Totsecs/8 );
  770.     else if (Verbose>1)
  771.         fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 );
  772.     for (attempts=0; attempts <= RETRYMAX; attempts++) {
  773.         Lastrx= firstch;
  774.         sendline(cseclen==1024?STX:SOH);
  775.         sendline(sectnum);
  776.         sendline(-sectnum -1);
  777.         oldcrc=checksum=0;
  778.         for (wcj=cseclen,cp=buf; --wcj>=0; ) {
  779.             sendline(*cp);
  780.             oldcrc=updcrc((0377& *cp), oldcrc);
  781.             checksum += *cp++;
  782.         }
  783.         if (Crcflg) {
  784.             oldcrc=updcrc(0,updcrc(0,oldcrc));
  785.             sendline((int)oldcrc>>8);
  786.             sendline((int)oldcrc);
  787.         }
  788.         else
  789.             sendline(checksum);
  790.         flushmo();
  791.  
  792.         if (Optiong) {
  793.             firstsec = FALSE; return OK;
  794.         }
  795.         firstch = readline(Rxtimeout);
  796. gotnak:
  797.         switch (firstch) {
  798.         case CAN:
  799.             if(Lastrx == CAN) {
  800. cancan:
  801.                 zperr("Cancelled");  return ERROR;
  802.             }
  803.             break;
  804.         case TIMEOUT:
  805.             zperr("Timeout on sector ACK"); continue;
  806.         case WANTCRC:
  807.             if (firstsec)
  808.                 Crcflg = TRUE;
  809.         case NAK:
  810.             zperr("NAK on sector"); continue;
  811.         case ACK: 
  812.             firstsec=FALSE;
  813.             Totsecs += (cseclen>>7);
  814.             return OK;
  815.         case ERROR:
  816.             zperr("Got burst for sector ACK"); break;
  817.         default:
  818.             zperr("Got %02x for sector ACK", firstch); break;
  819.         }
  820.         for (;;) {
  821.             Lastrx = firstch;
  822.             if ((firstch = readline(Rxtimeout)) == TIMEOUT)
  823.                 break;
  824.             if (firstch == NAK || firstch == WANTCRC)
  825.                 goto gotnak;
  826.             if (firstch == CAN && Lastrx == CAN)
  827.                 goto cancan;
  828.         }
  829.     }
  830.     zperr("Retry Count Exceeded");
  831.     return ERROR;
  832. }
  833.  
  834. /* fill buf with count chars padding with ^Z for CPM */
  835. filbuf(buf, count)
  836. register char *buf;
  837. {
  838.     register c, m;
  839.  
  840.     if ( !Ascii) {
  841.         m = read(fileno(in), buf, count);
  842.         if (m <= 0)
  843.             return 0;
  844.         while (m < count)
  845.             buf[m++] = 032;
  846.         return count;
  847.     }
  848.     m=count;
  849.     if (Lfseen) {
  850.         *buf++ = 012; --m; Lfseen = 0;
  851.     }
  852.     while ((c=getc(in))!=EOF) {
  853.         if (c == 012) {
  854.             *buf++ = 015;
  855.             if (--m == 0) {
  856.                 Lfseen = TRUE; break;
  857.             }
  858.         }
  859.         *buf++ =c;
  860.         if (--m == 0)
  861.             break;
  862.     }
  863.     if (m==count)
  864.         return 0;
  865.     else
  866.         while (--m>=0)
  867.             *buf++ = CPMEOF;
  868.     return count;
  869. }
  870.  
  871. /* Fill buffer with blklen chars */
  872. zfilbuf()
  873. {
  874.     int n;
  875.  
  876. #ifdef TXBSIZE
  877.     vfile("zfilbuf: bytcnt =%lu vpos=%lu blklen=%d", bytcnt, vpos, blklen);
  878.     /* We assume request is within buffer, or just beyond */
  879.     txbuf = Txb + (bytcnt & TXBMASK);
  880.     if (vpos <= bytcnt) {
  881.         n = fread(txbuf, 1, blklen, in);
  882.  
  883.         vpos += n;
  884.         if (n < blklen)
  885.             Eofseen = 1;
  886.         vfile("zfilbuf: n=%d vpos=%lu Eofseen=%d", n, vpos, Eofseen);
  887.         return n;
  888.     }
  889.     if (vpos >= (bytcnt+blklen))
  890.         return blklen;
  891.     /* May be a short block if crash recovery etc. */
  892.     Eofseen = BEofseen;
  893.     return (vpos - bytcnt);
  894. #else
  895.     n = fread(txbuf, 1, blklen, in);
  896.     if (n < blklen)
  897.         Eofseen = 1;
  898.     return n;
  899. #endif
  900. }
  901.  
  902. #ifdef TXBSIZE
  903. /* Replacement for brain damaged fseek function.  Returns 0==success */
  904. fooseek(fptr, pos, whence)
  905. FILE *fptr;
  906. long pos;
  907. {
  908.     long m, n;
  909.  
  910.     vfile("fooseek: pos =%lu vpos=%lu Canseek=%d", pos, vpos, Canseek);
  911.     /* Seek offset < current buffer */
  912.     if (pos < (vpos -TXBSIZE +1024)) {
  913.         BEofseen = 0;
  914.         if (Canseek > 0) {
  915.             vpos = pos & ~TXBMASK;
  916.             if (vpos >= pos)
  917.                 vpos -= TXBSIZE;
  918.             if (fseek(fptr, vpos, 0))
  919.                 return 1;
  920.         }
  921.         else if (Canseek == 0) {
  922.             if (fseek(fptr, vpos = 0L, 0))
  923.                 return 1;
  924.         } else
  925.             return 1;
  926.         while (vpos < pos) {
  927.             n = fread(Txb, 1, TXBSIZE, fptr);
  928.             vpos += n;
  929.             vfile("n=%d vpos=%ld", n, vpos);
  930.             if (n < TXBSIZE) {
  931.                 BEofseen = 1;
  932.                 break;
  933.             }
  934.         }
  935.         vfile("vpos=%ld", vpos);
  936.         return 0;
  937.     }
  938.     /* Seek offset > current buffer (Crash Recovery, etc.) */
  939.     if (pos > vpos) {
  940.         if (Canseek)
  941.             if (fseek(fptr, vpos = (pos & ~TXBMASK), 0))
  942.                 return 1;
  943.         while (vpos <= pos) {
  944.             txbuf = Txb + (vpos & TXBMASK);
  945.             m = TXBSIZE - (vpos & TXBMASK);
  946.             vfile("m=%ld vpos=%ld", m,vpos);
  947.                 n = fread(txbuf, 1, m, fptr);
  948.             vfile("n=%ld vpos=%ld", n,vpos);
  949.             vpos += n;
  950.             vfile("bo=%d m=%ld vpos=%ld", txbuf-Txb,m,vpos);
  951.             if (n < m) {
  952.                 BEofseen = 1;
  953.                 break;
  954.             }
  955.         }
  956.         return 0;
  957.     }
  958.     /* Seek offset is within current buffer */
  959.     vfile("within buffer: vpos=%ld", vpos);
  960.     return 0;
  961. }
  962. #define fseek fooseek
  963. #endif
  964.  
  965.  
  966. /* VARARGS1 */
  967. vfile(f, a, b, c, d)
  968. char *f;
  969. long a, b, c, d;
  970. {
  971.     if (Verbose > 2) {
  972.         fprintf(stderr, f, a, b, c, d);
  973.         fprintf(stderr, "\n");
  974.     }
  975. }
  976.  
  977.  
  978. alrm()
  979. {
  980.     longjmp(tohere, -1);
  981. }
  982.  
  983.  
  984. /*
  985.  * readline(timeout) reads character(s) from file descriptor 0
  986.  * timeout is in tenths of seconds
  987.  */
  988. readline(timeout)
  989. {
  990.     register int c;
  991.     static char byt[1];
  992.  
  993.     fflush(stdout);
  994.     if (setjmp(tohere)) {
  995.         zperr("TIMEOUT");
  996.         return TIMEOUT;
  997.     }
  998.     c = timeout/10;
  999.     if (c<2)
  1000.         c=2;
  1001.     if (Verbose>5) {
  1002.         fprintf(stderr, "Timeout=%d Calling alarm(%d) ", timeout, c);
  1003.     }
  1004.     signal(SIGALRM, alrm); alarm(c);
  1005.     c=read(0, byt, 1);
  1006.     alarm(0);
  1007.     if (Verbose>5)
  1008.         fprintf(stderr, "ret %x\n", byt[0]);
  1009.     if (c<1)
  1010.         return TIMEOUT;
  1011.     return (byt[0]&0377);
  1012. }
  1013.  
  1014. flushmo()
  1015. {
  1016.     fflush(stdout);
  1017. }
  1018.  
  1019.  
  1020. purgeline()
  1021. {
  1022. #ifdef USG
  1023.     ioctl(0, TCFLSH, 0);
  1024. #else
  1025.     lseek(0, 0L, 2);
  1026. #endif
  1027. }
  1028.  
  1029. /* send cancel string to get the other end to shut up */
  1030. canit()
  1031. {
  1032.     static char canistr[] = {
  1033.      24,24,24,24,24,24,24,24,24,24,8,8,8,8,8,8,8,8,8,8,0
  1034.     };
  1035.  
  1036.     printf(canistr);
  1037.     fflush(stdout);
  1038. }
  1039.  
  1040.  
  1041. /*
  1042.  * Log an error
  1043.  */
  1044. /*VARARGS1*/
  1045. zperr(s,p,u)
  1046. char *s, *p, *u;
  1047. {
  1048.     if (Verbose <= 0)
  1049.         return;
  1050.     fprintf(stderr, "Retry %d: ", errors);
  1051.     fprintf(stderr, s, p, u);
  1052.     fprintf(stderr, "\n");
  1053. }
  1054.  
  1055. /*
  1056.  * substr(string, token) searches for token in string s
  1057.  * returns pointer to token within string if found, NULL otherwise
  1058.  */
  1059. char *
  1060. substr(s, t)
  1061. register char *s,*t;
  1062. {
  1063.     register char *ss,*tt;
  1064.     /* search for first char of token */
  1065.     for (ss=s; *s; s++)
  1066.         if (*s == *t)
  1067.             /* compare token with substring */
  1068.             for (ss=s,tt=t; ;) {
  1069.                 if (*tt == 0)
  1070.                     return s;
  1071.                 if (*ss++ != *tt++)
  1072.                     break;
  1073.             }
  1074.     return NULL;
  1075. }
  1076.  
  1077. char *babble[] = {
  1078.     "Send file(s) with ZMODEM/YMODEM/XMODEM Protocol",
  1079.     "    (Y) = Option applies to YMODEM only",
  1080.     "    (Z) = Option applies to ZMODEM only",
  1081.     "Usage:    sz [-2+abdefkLlNnquvwYy] [-] file ...",
  1082.     "    sz [-2Ceqv] -c COMMAND",
  1083.     "    sb [-2adfkquv] [-] file ...",
  1084.     "    sx [-2akquv] [-] file",
  1085. #ifdef CSTOPB
  1086.     "    2   Use 2 stop bits",
  1087. #endif
  1088.     "    +   Append to existing destination file (Z)",
  1089.     "    a   (ASCII) change NL to CR/LF",
  1090.     "    b   Binary file transfer override",
  1091.     "    c   send COMMAND (Z)",
  1092.     "    d   Change '.' to '/' in pathnames (Y/Z)",
  1093.     "    e   Escape all control characters (Z)",
  1094.     "    f   send Full pathname (Y/Z)",
  1095.     "    i   send COMMAND, ack Immediately (Z)",
  1096.     "    k   Send 1024 byte packets (Y)",
  1097.     "    L N Limit subpacket length to N bytes (Z)",
  1098.     "    l N Limit frame length to N bytes (l>=L) (Z)",
  1099.     "    n   send file only if source newer (Z)",
  1100.     "    N   send file only if source newer or longer (Z)",
  1101.     "    o   Use 16 bit CRC instead of 32 bit CRC (Z)",
  1102.     "    p   Protect existing destination file (Z)",
  1103.     "    r   Resume/Recover interrupted file transfer (Z)",
  1104.     "    q   Quiet (no progress reports)",
  1105.     "    u   Unlink (remove) file after transmission",
  1106.     "    v   Verbose - provide debugging information",
  1107.     "    w N restrict Window to N bytes (Z)",
  1108.     "    Y   Yes, overwrite existing file, skip if not present at rx (Z)",
  1109.     "    y   Yes, overwrite existing file (Z)",
  1110.     "    Z   Activate ZMODEM compression(Z)",
  1111.     ""
  1112. };
  1113.  
  1114. usage()
  1115. {
  1116.     char **pp;
  1117.  
  1118.     for (pp=babble; **pp; ++pp)
  1119.         fprintf(stderr, "%s\n", *pp);
  1120.     fprintf(stderr, "%s for %s by Chuck Forsberg, Omen Technology INC\n",
  1121.      VERSION, OS);
  1122.     fprintf(stderr, "\t\t\042The High Reliability Software\042\n");
  1123.     cucheck();
  1124.     exit(0);
  1125. }
  1126.  
  1127. /*
  1128.  * Get the receiver's init parameters
  1129.  */
  1130. getzrxinit()
  1131. {
  1132.     register n;
  1133.     struct stat f;
  1134.  
  1135.     for (n=10; --n>=0; ) {
  1136.         
  1137.         switch (zgethdr(Rxhdr, 1)) {
  1138.         case ZCHALLENGE:    /* Echo receiver's challenge numbr */
  1139.             stohdr(Rxpos);
  1140.             zshhdr(4, ZACK, Txhdr);
  1141.             continue;
  1142.         case ZCOMMAND:        /* They didn't see out ZRQINIT */
  1143.             stohdr(0L);
  1144.             zshhdr(4, ZRQINIT, Txhdr);
  1145.             continue;
  1146.         case ZRINIT:
  1147.             Rxflags = 0377 & Rxhdr[ZF0];
  1148.             Usevhdrs = Rxhdr[ZF1] & CANVHDR;
  1149.             Txfcs32 = (Wantfcs32 && (Rxflags & CANFC32));
  1150.             Zctlesc |= Rxflags & TESCCTL;
  1151.             Rxbuflen = (0377 & Rxhdr[ZP0])+((0377 & Rxhdr[ZP1])<<8);
  1152.             if ( !(Rxflags & CANFDX))
  1153.                 Txwindow = 0;
  1154.             vfile("Rxbuflen=%d Tframlen=%d", Rxbuflen, Tframlen);
  1155.             if ( !Fromcu)
  1156.                 signal(SIGINT, SIG_IGN);
  1157. #ifdef MODE2OK
  1158.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  1159. #endif
  1160.  
  1161. #ifndef READCHECK
  1162. #ifndef USG
  1163.             /* Use 1024 byte frames if no sample/interrupt */
  1164.             if (Rxbuflen < 32 || Rxbuflen > 1024) {
  1165.                 Rxbuflen = 1024;
  1166.                 vfile("Rxbuflen=%d", Rxbuflen);
  1167.             }
  1168. #endif
  1169. #endif
  1170.  
  1171.             /* Override to force shorter frame length */
  1172.             if (Rxbuflen && (Rxbuflen>Tframlen) && (Tframlen>=32))
  1173.                 Rxbuflen = Tframlen;
  1174.             if ( !Rxbuflen && (Tframlen>=32) && (Tframlen<=1024))
  1175.                 Rxbuflen = Tframlen;
  1176.             vfile("Rxbuflen=%d", Rxbuflen);
  1177.  
  1178.             /* If using a pipe for testing set lower buf len */
  1179.             fstat(0, &f);
  1180.             if ((f.st_mode & S_IFMT) != S_IFCHR) {
  1181.                 Rxbuflen = 1024;
  1182.             }
  1183.  
  1184.  
  1185.             /*
  1186.              * If input is not a regular file, force ACK's to
  1187.              *  prevent running beyond the buffer limits
  1188.              */
  1189.             if ( !Command) {
  1190.                 fstat(fileno(in), &f);
  1191.                 if ((f.st_mode & S_IFMT) != S_IFREG) {
  1192.                     Canseek = -1;
  1193. #ifdef TXBSIZE
  1194.                     Txwindow = TXBSIZE - 1024;
  1195.                     Txwspac = TXBSIZE/4;
  1196. #else
  1197.                     return ERROR;
  1198. #endif
  1199.                 }
  1200.             }
  1201.  
  1202.             /* Set initial subpacket length */
  1203.             if (blklen < 1024) {    /* Command line override? */
  1204.                 if (Effbaud > 300)
  1205.                     blklen = 256;
  1206.                 if (Effbaud > 1200)
  1207.                     blklen = 512;
  1208.                 if (Effbaud > 2400)
  1209.                     blklen = 1024;
  1210.             }
  1211.             if (Rxbuflen && blklen>Rxbuflen)
  1212.                 blklen = Rxbuflen;
  1213.             if (blkopt && blklen > blkopt)
  1214.                 blklen = blkopt;
  1215.             vfile("Rxbuflen=%d blklen=%d", Rxbuflen, blklen);
  1216.             vfile("Txwindow = %u Txwspac = %d", Txwindow, Txwspac);
  1217.  
  1218.  
  1219.             if (Lztrans == ZTRLE && (Rxflags & CANRLE))
  1220.                 Txfcs32 = 2;
  1221.             else
  1222.                 Lztrans = 0;
  1223.  
  1224.             return (sendzsinit());
  1225.         case ZCAN:
  1226.         case TIMEOUT:
  1227.             return ERROR;
  1228.         case ZRQINIT:
  1229.             if (Rxhdr[ZF0] == ZCOMMAND)
  1230.                 continue;
  1231.         default:
  1232.             zshhdr(4, ZNAK, Txhdr);
  1233.             continue;
  1234.         }
  1235.     }
  1236.     return ERROR;
  1237. }
  1238.  
  1239. /* Send send-init information */
  1240. sendzsinit()
  1241. {
  1242.     register c;
  1243.  
  1244.     if (Myattn[0] == '\0' && (!Zctlesc || (Rxflags & TESCCTL)))
  1245.         return OK;
  1246.     errors = 0;
  1247.     for (;;) {
  1248.         stohdr(0L);
  1249. #ifdef ALTCANOFF
  1250.         Txhdr[ALTCOFF] = ALTCANOFF;
  1251. #endif
  1252.         if (Zctlesc) {
  1253.             Txhdr[ZF0] |= TESCCTL; zshhdr(4, ZSINIT, Txhdr);
  1254.         }
  1255.         else
  1256.             zsbhdr(4, ZSINIT, Txhdr);
  1257.         zsdata(Myattn, ZATTNLEN, ZCRCW);
  1258.         c = zgethdr(Rxhdr, 1);
  1259.         switch (c) {
  1260.         case ZCAN:
  1261.             return ERROR;
  1262.         case ZACK:
  1263.             return OK;
  1264.         default:
  1265.             if (++errors > 19)
  1266.                 return ERROR;
  1267.             continue;
  1268.         }
  1269.     }
  1270. }
  1271.  
  1272. /* Send file name and related info */
  1273. zsendfile(buf, blen)
  1274. char *buf;
  1275. {
  1276.     register c;
  1277.     register UNSL long crc;
  1278.     long lastcrcrq = -1;
  1279.     char *p;
  1280.  
  1281.     for (errors=0; ++errors<11;) {
  1282.         Txhdr[ZF0] = Lzconv;    /* file conversion request */
  1283.         Txhdr[ZF1] = Lzmanag;    /* file management request */
  1284.         if (Lskipnocor)
  1285.             Txhdr[ZF1] |= ZMSKNOLOC;
  1286.         Txhdr[ZF2] = Lztrans;    /* file transport request */
  1287.         Txhdr[ZF3] = 0;
  1288.         zsbhdr(4, ZFILE, Txhdr);
  1289.         zsdata(buf, blen, ZCRCW);
  1290. again:
  1291.         c = zgethdr(Rxhdr, 1);
  1292.         switch (c) {
  1293.         case ZRINIT:
  1294.             while ((c = readline(50)) > 0)
  1295.                 if (c == ZPAD) {
  1296.                     goto again;
  1297.                 }
  1298.             continue;
  1299.         case ZCAN:
  1300.         case TIMEOUT:
  1301.         case ZABORT:
  1302.         case ZFIN:
  1303.             sprintf(endmsg, "Got %s on pathname", frametypes[c+FTOFFSET]);
  1304.             return ERROR;
  1305.         default:
  1306.             sprintf(endmsg, "Got %d frame type on pathname", c);
  1307.             continue;
  1308.         case ERROR:
  1309.         case ZNAK:
  1310.             continue;
  1311.         case ZCRC:
  1312.             if (Rxpos != lastcrcrq) {
  1313.                 lastcrcrq = Rxpos;
  1314.                 crc = 0xFFFFFFFFL;
  1315.                 if (Canseek >= 0) {
  1316.                     fseek(in, 0L, 0);
  1317.                     while (((c = getc(in)) != EOF) && --lastcrcrq)
  1318.                         crc = UPDC32(c, crc);
  1319.                     crc = ~crc;
  1320.                     clearerr(in);    /* Clear possible EOF */
  1321.                     lastcrcrq = Rxpos;
  1322.                 }
  1323.             }
  1324.             stohdr(crc);
  1325.             zsbhdr(4, ZCRC, Txhdr);
  1326.             goto again;
  1327.         case ZFERR:
  1328.         case ZSKIP:
  1329.             sprintf(endmsg, "File skipped by receiver request");
  1330.             fclose(in); return c;
  1331.         case ZRPOS:
  1332.             /*
  1333.              * Suppress zcrcw request otherwise triggered by
  1334.              * lastyunc==bytcnt
  1335.              */
  1336.             if (fseek(in, Rxpos, 0))
  1337.                 return ERROR;
  1338.             Lastsync = (bytcnt = Txpos = Lrxpos = Rxpos) -1;
  1339.             return zsendfdata();
  1340.         }
  1341.     }
  1342.     fclose(in); return ERROR;
  1343. }
  1344.  
  1345. /* Send the data in the file */
  1346. zsendfdata()
  1347. {
  1348.     register c, e, n;
  1349.     register newcnt;
  1350.     register long tcount = 0;
  1351.     int junkcount;        /* Counts garbage chars received by TX */
  1352.     static int tleft = 6;    /* Counter for test mode */
  1353.  
  1354.     junkcount = 0;
  1355.     Beenhereb4 = FALSE;
  1356. somemore:
  1357.     if (setjmp(intrjmp)) {
  1358. waitack:
  1359.         junkcount = 0;
  1360.         c = getinsync(0);
  1361. gotack:
  1362.         switch (c) {
  1363.         default:
  1364.         case ZCAN:
  1365.             fclose(in);
  1366.             return ERROR;
  1367.         case ZSKIP:
  1368.             fclose(in);
  1369.             return c;
  1370.         case ZACK:
  1371.         case ZRPOS:
  1372.             break;
  1373.         case ZRINIT:
  1374.             fclose(in);
  1375.             return OK;
  1376.         }
  1377. #ifdef READCHECK
  1378.         /*
  1379.          * If the reverse channel can be tested for data,
  1380.          *  this logic may be used to detect error packets
  1381.          *  sent by the receiver, in place of setjmp/longjmp
  1382.          *  rdchk(fd) returns non 0 if a character is available
  1383.          */
  1384.         while (rdchk(0)) {
  1385. #ifdef EATSIT
  1386.             switch (checked)
  1387. #else
  1388.             switch (readline(1))
  1389. #endif
  1390.             {
  1391.             case CAN:
  1392.             case ZPAD:
  1393.                 c = getinsync(1);
  1394.                 goto gotack;
  1395.             case XOFF:        /* Wait a while for an XON */
  1396.             case XOFF|0200:
  1397.                 readline(100);
  1398.             }
  1399.         }
  1400. #endif
  1401.     }
  1402.  
  1403.     if ( !Fromcu)
  1404.         signal(SIGINT, onintr);
  1405.     newcnt = Rxbuflen;
  1406.     Txwcnt = 0;
  1407.     stohdr(Txpos);
  1408.     zsbhdr(4, ZDATA, Txhdr);
  1409.  
  1410.     /*
  1411.      * Special testing mode.  This should force receiver to Attn,ZRPOS
  1412.      *  many times.  Each time the signal should be caught, causing the
  1413.      *  file to be started over from the beginning.
  1414.      */
  1415.     if (Test) {
  1416.         if ( --tleft)
  1417.             while (tcount < 20000) {
  1418.                 printf(qbf); fflush(stdout);
  1419.                 tcount += strlen(qbf);
  1420. #ifdef READCHECK
  1421.                 while (rdchk(0)) {
  1422. #ifdef EATSIT
  1423.                     switch (checked)
  1424. #else
  1425.                     switch (readline(1))
  1426. #endif
  1427.                     {
  1428.                     case CAN:
  1429.                     case ZPAD:
  1430. #ifdef TCFLSH
  1431.                         ioctl(0, TCFLSH, 1);
  1432. #endif
  1433.                         goto waitack;
  1434.                     case XOFF:    /* Wait for XON */
  1435.                     case XOFF|0200:
  1436.                         readline(100);
  1437.                     }
  1438.                 }
  1439. #endif
  1440.             }
  1441.         signal(SIGINT, SIG_IGN); canit();
  1442.         sleep(3); purgeline(); mode(0);
  1443.         printf("\nsz: Tcount = %ld\n", tcount);
  1444.         if (tleft) {
  1445.             printf("ERROR: Interrupts Not Caught\n");
  1446.             exit(1);
  1447.         }
  1448.         exit(0);
  1449.     }
  1450.  
  1451.     do {
  1452.         n = zfilbuf();
  1453.         if (Eofseen)
  1454.             e = ZCRCE;
  1455.         else if (junkcount > 3)
  1456.             e = ZCRCW;
  1457.         else if (bytcnt == Lastsync)
  1458.             e = ZCRCW;
  1459.         else if (Rxbuflen && (newcnt -= n) <= 0)
  1460.             e = ZCRCW;
  1461.         else if (Txwindow && (Txwcnt += n) >= Txwspac) {
  1462.             Txwcnt = 0;  e = ZCRCQ;
  1463.         } else
  1464.             e = ZCRCG;
  1465.         if (Verbose>1)
  1466.             fprintf(stderr, "\r%7ld ZMODEM%s    ",
  1467.               Txpos, Crc32t?" CRC-32":"");
  1468.         zsdata(txbuf, n, e);
  1469.         bytcnt = Txpos += n;
  1470.         if (e == ZCRCW)
  1471.             goto waitack;
  1472. #ifdef READCHECK
  1473.         /*
  1474.          * If the reverse channel can be tested for data,
  1475.          *  this logic may be used to detect error packets
  1476.          *  sent by the receiver, in place of setjmp/longjmp
  1477.          *  rdchk(fd) returns non 0 if a character is available
  1478.          */
  1479.         fflush(stdout);
  1480.         while (rdchk(0)) {
  1481. #ifdef EATSIT
  1482.             switch (checked)
  1483. #else
  1484.             switch (readline(1))
  1485. #endif
  1486.             {
  1487.             case CAN:
  1488.             case ZPAD:
  1489.                 c = getinsync(1);
  1490.                 if (c == ZACK)
  1491.                     break;
  1492. #ifdef TCFLSH
  1493.                 ioctl(0, TCFLSH, 1);
  1494. #endif
  1495.                 /* zcrce - dinna wanna starta ping-pong game */
  1496.                 zsdata(txbuf, 0, ZCRCE);
  1497.                 goto gotack;
  1498.             case XOFF:        /* Wait a while for an XON */
  1499.             case XOFF|0200:
  1500.                 readline(100);
  1501.             default:
  1502.                 ++junkcount;
  1503.             }
  1504.         }
  1505. #endif    /* READCHECK */
  1506.         if (Txwindow) {
  1507.             while ((tcount = (Txpos - Lrxpos)) >= Txwindow) {
  1508.                 vfile("%ld window >= %u", tcount, Txwindow);
  1509.                 if (e != ZCRCQ)
  1510.                     zsdata(txbuf, 0, e = ZCRCQ);
  1511.                 c = getinsync(1);
  1512.                 if (c != ZACK) {
  1513. #ifdef TCFLSH
  1514.                     ioctl(0, TCFLSH, 1);
  1515. #endif
  1516.                     zsdata(txbuf, 0, ZCRCE);
  1517.                     goto gotack;
  1518.                 }
  1519.             }
  1520.             vfile("window = %ld", tcount);
  1521.         }
  1522.     } while (!Eofseen);
  1523.     if ( !Fromcu)
  1524.         signal(SIGINT, SIG_IGN);
  1525.  
  1526.     for (;;) {
  1527.         stohdr(Txpos);
  1528.         zsbhdr(4, ZEOF, Txhdr);
  1529.         switch (getinsync(0)) {
  1530.         case ZACK:
  1531.             continue;
  1532.         case ZRPOS:
  1533.             goto somemore;
  1534.         case ZRINIT:
  1535.             fclose(in);
  1536.             return OK;
  1537.         case ZSKIP:
  1538.             fclose(in);
  1539.             sprintf(endmsg, "File skipped by receiver request");
  1540.             return c;
  1541.         default:
  1542.             sprintf(endmsg, "Got %d trying to send end of file", c);
  1543.             fclose(in);
  1544.             return ERROR;
  1545.         }
  1546.     }
  1547. }
  1548.  
  1549. /*
  1550.  * Respond to receiver's complaint, get back in sync with receiver
  1551.  */
  1552. getinsync(flag)
  1553. {
  1554.     register c;
  1555.  
  1556.     for (;;) {
  1557.         if (Test) {
  1558.             printf("\r\n\n\n***** Signal Caught *****\r\n");
  1559.             Rxpos = 0; c = ZRPOS;
  1560.         } else
  1561.             c = zgethdr(Rxhdr, 0);
  1562.         switch (c) {
  1563.         case ZCAN:
  1564.         case ZABORT:
  1565.         case ZFIN:
  1566.         case TIMEOUT:
  1567.             sprintf(endmsg, "Got %s sending data", frametypes[c+FTOFFSET]);
  1568.             return ERROR;
  1569.         case ZRPOS:
  1570.             /* ************************************* */
  1571.             /*  If sending to a buffered modem, you  */
  1572.             /*   might send a break at this point to */
  1573.             /*   dump the modem's buffer.         */
  1574.             clearerr(in);    /* In case file EOF seen */
  1575.             if (fseek(in, Rxpos, 0))
  1576.                 return ERROR;
  1577.             Eofseen = 0;
  1578.             bytcnt = Lrxpos = Txpos = Rxpos;
  1579.             if (Lastsync == Rxpos) {
  1580.                 if (++Beenhereb4 > 12) {
  1581.                     sprintf(endmsg, "Can't send block");
  1582.                     return ERROR;
  1583.                 }
  1584.                 if (Beenhereb4 > 4)
  1585.                     if (blklen > 32)
  1586.                         blklen /= 2;
  1587.             }
  1588.             Lastsync = Rxpos;
  1589.             return c;
  1590.         case ZACK:
  1591.             Lrxpos = Rxpos;
  1592.             if (flag || Txpos == Rxpos)
  1593.                 return ZACK;
  1594.             continue;
  1595.         case ZRINIT:
  1596.             return c;
  1597.         case ZSKIP:
  1598.             sprintf(endmsg, "File skipped by receiver request");
  1599.             return c;
  1600.         case ERROR:
  1601.         default:
  1602.             zsbhdr(4, ZNAK, Txhdr);
  1603.             continue;
  1604.         }
  1605.     }
  1606. }
  1607.  
  1608.  
  1609. /* Say "bibi" to the receiver, try to do it cleanly */
  1610. saybibi()
  1611. {
  1612.     for (;;) {
  1613.         stohdr(0L);        /* CAF Was zsbhdr - minor change */
  1614.         zshhdr(4, ZFIN, Txhdr);    /*  to make debugging easier */
  1615.         switch (zgethdr(Rxhdr, 0)) {
  1616.         case ZFIN:
  1617.             sendline('O'); sendline('O'); flushmo();
  1618.         case ZCAN:
  1619.         case TIMEOUT:
  1620.             return;
  1621.         }
  1622.     }
  1623. }
  1624.  
  1625. /* Local screen character display function */
  1626. bttyout(c)
  1627. {
  1628.     if (Verbose)
  1629.         putc(c, stderr);
  1630. }
  1631.  
  1632. /* Send command and related info */
  1633. zsendcmd(buf, blen)
  1634. char *buf;
  1635. {
  1636.     register c;
  1637.     long cmdnum;
  1638.  
  1639.     cmdnum = getpid();
  1640.     errors = 0;
  1641.     for (;;) {
  1642.         stohdr(cmdnum);
  1643.         Txhdr[ZF0] = Cmdack1;
  1644.         zsbhdr(4, ZCOMMAND, Txhdr);
  1645.         zsdata(buf, blen, ZCRCW);
  1646. listen:
  1647.         Rxtimeout = 100;        /* Ten second wait for resp. */
  1648.         Usevhdrs = 0;        /* Allow rx to send fixed len headers */
  1649.         c = zgethdr(Rxhdr, 1);
  1650.  
  1651.         switch (c) {
  1652.         case ZRINIT:
  1653.             goto listen;    /* CAF 8-21-87 */
  1654.         case ERROR:
  1655.         case GCOUNT:
  1656.         case TIMEOUT:
  1657.             if (++errors > Cmdtries)
  1658.                 return ERROR;
  1659.             continue;
  1660.         case ZCAN:
  1661.         case ZABORT:
  1662.         case ZFIN:
  1663.         case ZSKIP:
  1664.         case ZRPOS:
  1665.             return ERROR;
  1666.         default:
  1667.             if (++errors > 20)
  1668.                 return ERROR;
  1669.             continue;
  1670.         case ZCOMPL:
  1671.             Exitcode = Rxpos;
  1672.             saybibi();
  1673.             return OK;
  1674.         case ZRQINIT:
  1675.             vfile("******** RZ *******");
  1676.             system("rz");
  1677.             vfile("******** SZ *******");
  1678.             goto listen;
  1679.         }
  1680.     }
  1681. }
  1682.  
  1683. /*
  1684.  * If called as sb use YMODEM protocol
  1685.  */
  1686. chkinvok(s)
  1687. char *s;
  1688. {
  1689.     register char *p;
  1690.  
  1691.     p = s;
  1692.     while (*p == '-')
  1693.         s = ++p;
  1694.     while (*p)
  1695.         if (*p++ == '/')
  1696.             s = p;
  1697.     if (*s == 'v') {
  1698.         Verbose=1; ++s;
  1699.     }
  1700.     Progname = s;
  1701.     if (s[0]=='s' && s[1]=='b') {
  1702.         Nozmodem = TRUE; blklen=1024;
  1703.     }
  1704.     if (s[0]=='s' && s[1]=='x') {
  1705.         Modem2 = TRUE;
  1706.     }
  1707. }
  1708.  
  1709. countem(argc, argv)
  1710. register char **argv;
  1711. {
  1712.     register c;
  1713.     struct stat f;
  1714.  
  1715.     for (Totalleft = 0, Filesleft = 0; --argc >=0; ++argv) {
  1716.         f.st_size = -1;
  1717.         if (Verbose>2) {
  1718.             fprintf(stderr, "\nCountem: %03d %s ", argc, *argv);
  1719.             fflush(stderr);
  1720.         }
  1721.         if (access(*argv, 04) >= 0 && stat(*argv, &f) >= 0) {
  1722.             c = f.st_mode & S_IFMT;
  1723.             if (c != S_IFDIR && c != S_IFBLK) {
  1724.                 ++Filesleft;  Totalleft += f.st_size;
  1725.             }
  1726.         }
  1727.         if (Verbose>2)
  1728.             fprintf(stderr, " %ld", f.st_size);
  1729.     }
  1730.     if (Verbose>2)
  1731.         fprintf(stderr, "\ncountem: Total %d %ld\n",
  1732.           Filesleft, Totalleft);
  1733. }
  1734.  
  1735. chartest(m)
  1736. {
  1737.     register n;
  1738.  
  1739.     mode(m);
  1740.     printf("\r\n\nCharacter Transparency Test Mode %d\r\n", m);
  1741.     printf("If Pro-YAM/ZCOMM is not displaying ^M hit ALT-V NOW.\r\n");
  1742.     printf("Hit Enter.\021");  fflush(stdout);
  1743.     readline(500);
  1744.  
  1745.     for (n = 0; n < 256; ++n) {
  1746.         if (!(n%8))
  1747.             printf("\r\n");
  1748.         printf("%02x ", n);  fflush(stdout);
  1749.         sendline(n);    flushmo();
  1750.         printf("  ");  fflush(stdout);
  1751.         if (n == 127) {
  1752.             printf("Hit Enter.\021");  fflush(stdout);
  1753.             readline(500);
  1754.             printf("\r\n");  fflush(stdout);
  1755.         }
  1756.     }
  1757.     printf("\021\r\nEnter Characters, echo is in hex.\r\n");
  1758.     printf("Hit SPACE or pause 40 seconds for exit.\r\n");
  1759.  
  1760.     while (n != TIMEOUT && n != ' ') {
  1761.         n = readline(400);
  1762.         printf("%02x\r\n", n);
  1763.         fflush(stdout);
  1764.     }
  1765.     printf("\r\nMode %d character transparency test ends.\r\n", m);
  1766.     fflush(stdout);
  1767. }
  1768.  
  1769. /* End of sz.c */
  1770.